﻿
using System;
using System.Collections.Generic;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public static class MLPloter
    {
        /// <summary>
        /// 添加空心多线（外轮廓-内轮廓-中线-内轮廓-外轮廓）
        /// </summary>
        /// <param name="St">起点</param>
        /// <param name="Ed">终点</param>
        /// <param name="Width">宽度</param>
        /// <param name="Thickness">厚度</param>
        /// <param name="Ls">轮廓图层</param>
        /// <param name="CutCurveA">起点边界</param>
        /// <param name="CutCurveB">重点边界</param>
        /// <param name="haveCenter">是否生成中心线</param>
        /// <returns></returns>
        public static void AddTube(Database db,Point2d St, Point2d Ed, ref Extents2d ext, double Width, double Thickness, string Ls,
            Curve CutCurveA = null, Curve CutCurveB = null, bool haveCenter = true)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                List<Line> LineSet = new List<Line>();
                DBObjectCollection Res = new DBObjectCollection();
                Line sideA, sideB, ccline, insideA, insideB;

                ccline = new Line(St.C3D(), Ed.C3D());
                sideA = (Line)ccline.GetOffsetCurves(-0.5 * Width)[0];
                sideB = (Line)ccline.GetOffsetCurves(0.5 * Width)[0];

                ccline.LayerId = lt["H中心线"];
                sideA.Layer = Ls;
                sideB.Layer = Ls;
                LineSet.Add(sideB);
                LineSet.Add(ccline);
                LineSet.Add(sideA);

                if (Thickness != 0)
                {
                    insideA = (Line)ccline.GetOffsetCurves(-0.5 * Width + Thickness)[0];
                    insideB = (Line)ccline.GetOffsetCurves(0.5 * Width - Thickness)[0];
                    insideA.LayerId = lt["H虚线"];
                    insideB.LayerId =lt["H虚线"];
                    LineSet.Add(insideA);
                    LineSet.Add(insideB);
                }

                foreach (var item in LineSet)
                {
                    Line updateLine;
                    if (item.Layer != "H中心线")
                    {
                        updateLine = item.UpdateBoundary(CutCurveA, CutCurveB);
                    }
                    else
                    {
                        if (haveCenter)
                        {
                            updateLine = item;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //Res.Add(updateLine);
                    modelSpace.AppendEntity(updateLine);
                    tr.AddNewlyCreatedDBObject(updateLine, true);
                    ext = ext.Add(new Extents2d(updateLine.Bounds.Value.MinPoint.C2D(), updateLine.Bounds.Value.MaxPoint.C2D()));
                }
               
                tr.Commit();
            }
        }
    }
}
